/*
 * Copyright (c) 2009, Monte Alto Research Center, All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Monte Alto Research Center ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Monte Alto Research Center
 */
package es.sonxurxo.androidrunner.view.web.session;

import java.util.List;

import org.apache.wicket.Request;
import org.apache.wicket.Session;
import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.authorization.strategies.role.Roles;
import org.springframework.security.Authentication;
import org.springframework.security.AuthenticationException;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;

import es.sonxurxo.androidrunner.model.service.player.to.LoginResultTO;
import es.sonxurxo.androidrunner.view.web.application.AndroidRunnerApplication;

public class AndroidRunnerSession extends AuthenticatedWebSession {
	
	private static final long serialVersionUID = -3080104955792651796L;

	private LoginResultTO loginResultTO;
	private Long observingGameId = null;
	private List<Long> excludedMessages = null;
	
	public AndroidRunnerSession(Request request) {
		super(request);
	}
	
	public static AndroidRunnerSession get() {
        return (AndroidRunnerSession) Session.get();
    }
	
	@Override
	public boolean authenticate(String username, String password) {
		boolean authenticated = false;
		try {
			Authentication authentication = AndroidRunnerApplication.get().getPlayerService().authenticate(
            		new UsernamePasswordAuthenticationToken(username, password));
            SecurityContextHolder.getContext().setAuthentication(authentication);
            authenticated = authentication.isAuthenticated();
            this.loginResultTO = (LoginResultTO) authentication.getPrincipal();
        } catch (AuthenticationException e) {
            authenticated = false;
        }
        return authenticated;

	}

	@Override
	public Roles getRoles() {
		Roles roles = new Roles();
		if (this.isSignedIn()) {
			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
			for (GrantedAuthority authority : authentication.getAuthorities()) {
	            roles.add(authority.getAuthority());
	        }
		}
		return roles;
	}
	
	@Override
	public void signOut() {
		SecurityContextHolder.getContext().setAuthentication(null);
        invalidate();
        super.signOut();
	}

	public LoginResultTO getLoginResultTO() {
		return this.loginResultTO;
	}

	public List<Long> getExcludedMessages() {
		return this.excludedMessages;
	}

	public void setExcludedMessages(List<Long> excludedMessages) {
		this.excludedMessages = excludedMessages;
	}

	public Long getObservingGameId() {
		return this.observingGameId;
	}

	public void setObservingGameId(Long observingGameId) {
		this.observingGameId = observingGameId;
	}
}
